<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>memset</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">Standard C String and Character</a> &gt; <a href=
    "memset.html">memset</a>
  </div>

  <div class="name-format">
    memset
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;string.h&gt;
  void* memset( void* buffer, int ch, size_t count );
</pre>

  <p>The function memset() copies <em>ch</em> into the first
  <em>count</em> characters of <em>buffer</em>, and returns
  <em>buffer</em>. memset() is useful for intializing a section of
  memory to some value. For example, this command:</p>

  <pre class="example-code">
   memset( the_array, &#39;\0&#39;, sizeof(the_array) );                
</pre>

  <p>...is a very efficient way to set all values of the_array to
  zero.</p>  

  <p>The table below compares two different methods for initializing
  an array of characters: a for-loop versus memset().  As the size of the
  data being initialized increases, memset() clearly gets the job done
  much more quickly:</p>

  <table class="misc-table">
    <tr class="misc-table-tr-1">
      <th>Input size</th>
      <th>Initialized with a for-loop</th>
      <th>Initialized with memset()</th>
    </tr>

    <tr class="misc-table-tr-2">
      <td>1000</td>
      <td>0.016</td>
      <td>0.017</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>10000</td>
      <td>0.055</td>
      <td>0.013</td>
    </tr>

    <tr class="misc-table-tr-2">
      <td>100000</td>
      <td>0.443</td>
      <td>0.029</td>
    </tr>

    <tr class="misc-table-tr-1">
      <td>1000000</td>
      <td>4.337</td>
      <td>0.291</td>
    </tr>
  </table>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="memcmp.html">memcmp</a><br>
    <a href="memcpy.html">memcpy</a><br>
    <a href="memmove.html">memmove</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
